
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Django | home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="创建表字段123456789101112131415161718192021222324252627282930313233343536373839from django.db import models# 导入时间域from django.utils import timezone# 基类class Base(models.Model):    #创建时间    create_time &amp;#x3D; models.DateTimeField(default&amp;#x3D;timezone.now,null&amp;#x3D;True)    class Meta:        abstract &amp;#x3D; True# 用户表c...">
    
        <link rel="icon" href="/favicon.ico">
    
    
        
            <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
        
            <link rel="stylesheet" href="/css/stage.css">
        
            <link rel="stylesheet" href="/css/avatar-bg.css">
        
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body>
<header id="header">
    <div class="menu">
        <i class="fa fa-bars"></i>
    </div>
    <div class="header-main">
        <h1><a href="/">home</a></h1>
    </div>
    <div id="nav">
        <div class="nav-img" id="nav-img"></div>
        <div class="sentences">
            天生我材必有用。——李白
        </div>
    </div>
</header>

<div id="content-outer">
    <div id="content-inner">
        <div class="clearfix">
    <article id="post">
        <h1>Django</h1>
        <div class="create">
            <span>Created</span>
            
                <time datetime="2020-04-20T17:32:11.000Z">
                    2020-04-21
                </time>
            
            
        </div>
        <h1 id="创建表字段"><a href="#创建表字段" class="headerlink" title="创建表字段"></a>创建表字段</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">from django.db import models</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 导入时间域</span><br><span class="line">from django.utils import timezone</span><br><span class="line"></span><br><span class="line"># 基类</span><br><span class="line">class Base(models.Model):</span><br><span class="line"></span><br><span class="line">    #创建时间</span><br><span class="line">    create_time &#x3D; models.DateTimeField(default&#x3D;timezone.now,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        abstract &#x3D; True</span><br><span class="line"></span><br><span class="line"># 用户表</span><br><span class="line">class User(Base):</span><br><span class="line"></span><br><span class="line">    #用户名</span><br><span class="line">    username &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #密码</span><br><span class="line">    password &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #头像</span><br><span class="line">    img &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #用户级别,0代表普通用户，1代表超级管理员，2网站编辑</span><br><span class="line">    usertype &#x3D; models.IntegerField(default&#x3D;0,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    #手机号</span><br><span class="line">    phone &#x3D; models.IntegerField()</span><br><span class="line"></span><br><span class="line">    #个人主页</span><br><span class="line">    num &#x3D; models.IntegerField(default&#x3D;0,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    #声明表名</span><br><span class="line">    class Meta:</span><br><span class="line">        db_table &#x3D; &#39;user&#39;</span><br></pre></td></tr></table></figure>

<h1 id="数据库迁移"><a href="#数据库迁移" class="headerlink" title="数据库迁移"></a>数据库迁移</h1><p>python manage.py makemigrations 项目名<br>python manage.py makemigrations myapp<br>注意：一定要加项目名称，不然myapp中models.py中的表就迁移不到数据库中<br>然后执行：python manage.py migrate</p>
<h5 id="在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py"><a href="#在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py" class="headerlink" title="在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01/models.py"></a>在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01/models.py</h5><p>）</p>
<h1 id="导包时注意事项"><a href="#导包时注意事项" class="headerlink" title="导包时注意事项"></a>导包时注意事项</h1><p>from 项目名.models import User<br>from myapp.models import User<br>在哪个项目中就用哪个项目名</p>
<h1 id="md5加密"><a href="#md5加密" class="headerlink" title="md5加密"></a>md5加密</h1><p>要将需要加密的变量进行强转，强转成字符串，然后进行编码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># md5加密方法</span><br><span class="line">def make_pasword(mypass):</span><br><span class="line"></span><br><span class="line">    #生成md5对象</span><br><span class="line">    md5 &#x3D; hashlib.md5()</span><br><span class="line"></span><br><span class="line">    #转码操作</span><br><span class="line">    mypass_utf8 &#x3D; str(mypass).encode(encoding&#x3D;&#39;utf-8&#39;)</span><br><span class="line"></span><br><span class="line">    #进行加密操作</span><br><span class="line">    md5.update(mypass_utf8)</span><br><span class="line"></span><br><span class="line">    #返回密文</span><br><span class="line">    return md5.hexdigest()</span><br></pre></td></tr></table></figure>



<p><img src="/hugo_blog/0d8a90abc2d78eaa5e01d3fb7f57e6da.jpg" alt="美图"></p>

        <div>
            <ul class="tags-category-list" itemprop="keywords"><li class="tags-category-list-item"><a class="tags-category-list-link" href="/hexo_blog/tags/python/" rel="tag">python</a></li></ul>
        </div>
        <div class="bottom-line"></div>
        
    <nav id="article-nav">
        
            <a href="/hexo_blog/2020/04/21/day002/" id="article-nav-newer" class="article-nav-link-wrap">
        <span class="article-nav-title">
            
                Hugo
            
        </span>
                <strong class="article-nav-caption">&gt;</strong>
            </a>
        
        
            <a href="/hexo_blog/2020/04/21/day03/" id="article-nav-older" class="article-nav-link-wrap">
                <strong class="article-nav-caption">&lt;</strong>
                <span class="article-nav-title">
                    
                        简易购物车
                </span>
                
            </a>
        
    </nav>


        
    </article>
    <div id="toc">
        
            <h2>Table of Contents</h2>
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#创建表字段"><span class="toc-number">1.</span> <span class="toc-text">创建表字段</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#数据库迁移"><span class="toc-number">2.</span> <span class="toc-text">数据库迁移</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py"><span class="toc-number">2.0.0.0.1.</span> <span class="toc-text">在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01&#x2F;models.py</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#导包时注意事项"><span class="toc-number">3.</span> <span class="toc-text">导包时注意事项</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#md5加密"><span class="toc-number">4.</span> <span class="toc-text">md5加密</span></a></li></ol>
        
    </div>
</div>

    </div>
</div>
<footer id="footer">
    <div id="copyright">&copy; Mr.Song  2020</div>
    <div id="theme">
        Powered by <a href="http://hexo.io" target="_blank" rel="noopener">Hexo</a>. Theme by <a href="https://github.com/markyong/hexo-theme-stage" target="_blank" rel="noopener">Stage</a>
    </div>
</footer>
<script src="/lib/js/waterrippleeffect.min.js"></script>
<script src="/js/header-bg.main.js"></script>

    <script src="/lib/js/smooth-scroll.min.js"></script>
    <script src="/js/toc.main.js"></script>

</body>
</html>
